/**
 *  MIT License
 *
 *  Copyright (c) Dmitry Makarenko 2019
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all
 *  copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *  SOFTWARE.
 */
#ifndef INTELHEX_H
#define INTELHEX_H

#include <vector>
//#include "std_compat.h"
#include <string>
#include <algorithm>
#include "Crc32.h"

namespace IntelHexNS {

enum class Result
{
    UNKNOWN,
    SUCCESS,
    FILE_NOT_FOUND,
    INCORRECT_FILE,
    UNSUPPORTED_FORMAT,
};

struct Block;

class IntelHex {
public:
    IntelHex();
    IntelHex(std::string path);
    ~IntelHex();
    std::string WStringToString(const std::wstring& ws);
    Result load(const std::string path);
    Result load(const std::wstring path);
    Result loads(const std::string &hex);
    Result save();
    Result save(std::wstring path);
    Result save(const std::string path) const;
    uint8_t get(uint32_t address) const;
    uint8_t &operator[](uint32_t address);
    void erase(uint32_t address, uint32_t length);
    uint32_t maxAddress() const;
    uint32_t minAddress() const;
    uint32_t size() const;
    Result state() const;
    void fillChar(uint32_t startAddr, uint32_t endAddr);
    void createBlock(uint32_t startAddr, uint32_t endAddr);
    void fill(uint8_t fillChar);
    bool isSet(uint32_t address, uint8_t &val);
    void transAddr();
    void transToAddr(uint32_t address);
    uint32_t calculateCRC(int crcAddr);
    void addCompFlag(uint32_t Addr,uint32_t Flags);

    bool insertData(uint32_t address, uint8_t val);
    void initBlock(uint32_t Addr, uint32_t length, uint8_t *data);
    void removeFlashDrv();

    void mergeHex(IntelHex *others);
    std::vector<Block> getBlocks();
    std::vector<Block *> m_blocks;
private:

    mutable Result m_state;
    std::string filename;
    Result parse(std::istream &input);
    uint8_t m_fillChar;
    uint32_t crc32Value;
};

} // namespace IntelHexNS
#endif
